package com.farm.utils_qiniuyun;

import java.util.Map;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.util.Auth;


public class UploadFile {

	public static String key(String img) {
        //...生成上传凭证，然后准备上传
		String imgUrl = "";
        String bucket = "jukou";
       /* String accessKey = "_jE5RN07r_TL8fWGKKJW94jv-bsJmlKQfEuiLtnz";
        String secretKey = "6o_jINRQnJsNOfFEJLCUzTnvyCjEtD1mBJyhMNaQ";*/
        String accessKey = "gHUr0wk1ruvjfaxyxKgg0G_-5poGxVpeXT_A9zqL";
        String secretKey = "Zk1hitP06ywSBDCEtoEsphrtHzl-XUtdM3DUCqMM";
        //构造一个带指定Zone对象的配置类

        /**
         *   华东	Zone.zone0()
		             华北	Zone.zone1()
		             华南	Zone.zone2()
		             北美	Zone.zoneNa0()
         */
        Configuration cfg = new Configuration(Zone.zone0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
//        String zxc = "E:\\手机备份\\华为V8\\DCIM\\Camera\\1_2_1.png";
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = img;
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        /*String key = null;*/
        
        String lastname = img.substring(img.lastIndexOf("/")+1,img.length());
        
        String key = lastname;
        key = CommonUtils.getUUID()+"_"+key;
        System.out.println("文件名:"+key);
        
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
        	System.out.println(localFilePath +"   :   "+key  + "  :  "+upToken );
            Response response = uploadManager.put(localFilePath, key, upToken);
            System.out.println(response.bodyString());
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            imgUrl = putRet.key;
//            System.out.println(putRet.key);
//            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        //String url = "http://p6ieexopw.bkt.clouddn.com/"+imgUrl;
		return imgUrl;
    }
	
	public static String netToQINIU(String img){
		//构造一个带指定Zone对象的配置类，Zone就是我们可选的存储区域
				Configuration cfg = new Configuration(Zone.zone0());
				//...其他参数参考类注释
				String bucket = "jukou";
			
				String accessKey = "gHUr0wk1ruvjfaxyxKgg0G_-5poGxVpeXT_A9zqL";
				String secretKey = "Zk1hitP06ywSBDCEtoEsphrtHzl-XUtdM3DUCqMM";
				//String key = null;
				
				 String lastname = img.substring(img.lastIndexOf("/")+1,img.length());
			        
			        String key = lastname;
			        key = CommonUtils.getUUID()+"_"+key;
			        System.out.println("文件名:"+key);
				
				String remoteSrcUrl = img;
				Auth auth = Auth.create(accessKey, secretKey);
				BucketManager bucketManager = new BucketManager(auth, cfg);
				//抓取网络资源到空间
				try {
				    FetchRet fetchRet = bucketManager.fetch(remoteSrcUrl, bucket, key);
				   /* System.out.println(fetchRet.hash);
				    System.out.println(fetchRet.key);
				    System.out.println(fetchRet.mimeType);
				    System.out.println(fetchRet.fsize);*/
				    
				    img = "https://annex.jukou.laidedui.com/"+fetchRet.key;
				} catch (QiniuException ex) {
					
					Response r = ex.response;
					System.err.println(r.toString());
					try {
						System.err.println(r.bodyString());
					} catch (QiniuException ex2) {
						//ignore
					}
				}
			return img;
	}
	
	
	public static void main(String[] args) {
		//System.out.println("ahah");
		String imgUrl = "http://jukou.laidedui.com/upload/wxcode/20191009/1570584320554_203.png";
		//String imgUrl = "C://Users/91578/Desktop/亿百汇.png";
		
		//String url = "annex.jukou.laidedui.com"+imgUrl;
		//String key = key(imgUrl);
		String key = netToQINIU(imgUrl);
		System.out.println(key);
	}
	
	
}
